﻿using KJSync.Access.Models;
using KJSyncXP.Access.Context;
using KJSyncXP.Oracle.Model;
using KJSyncXP.Sqlite.Model;
using KJSyncXP.SQLite.Context;
using log4net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KJSyncXP.Views;
using System.Windows.Input;
using KJSyncXP.Extensions;
using KJSyncXP.Services;

namespace KJSyncXP.ViewModels
{
    public class StandardViewModel : BaseViewModel 
    {

        private readonly SqliteDbContext _sqliteCtx;

        private readonly AccessDbContext _accessCtx;

        private readonly log4net.ILog _logger;

        // 声明命令
        public RelayCommand SearchBas001Command { get; private set; }

        public StandardViewModel(log4net.ILog log, SqliteDbContext sqliteCtx, AccessDbContext accessCtx)
        {
            _sqliteCtx = sqliteCtx;
            _accessCtx = accessCtx;
            _logger = log;
            SearchBas001Command = new RelayCommand(SearchBas001);
            LoadBas001();
        }

        // MES标准数据集合
        private ObservableCollection<Sqlite_KJ_TEST_STANDARD> _standard = new ObservableCollection<Sqlite_KJ_TEST_STANDARD>();
        public ObservableCollection<Sqlite_KJ_TEST_STANDARD> Standard
        {
            get => _standard;
            set
            {
                _standard = value;
                
            }
        }

        // 胶料品号信息表相关数据
        private ObservableCollection<Bas001> _bas001 = new ObservableCollection<Bas001>();
        public ObservableCollection<Bas001> Bas001
        {
            get => _bas001;
            set
            {
                _bas001 = value;
                // 触发UI更新
                RaisePropertyChanged("Bas001");
            }
        }

        // 选中的胶料品号项
        private Bas001 _selectedbas001;
        public Bas001 SelectedBas001
        {
            get => _selectedbas001;
            set
            {
                _selectedbas001 = value;
                // 触发UI更新
                RaisePropertyChanged("SelectedBas001");
                // 当选中项不为 null 时，调用加载方法
                if (value != null)
                {
                    LoadJoinSet001(value.Idnum);
                    LoadStrandards(value.Name1);
                }
               
            }
        }

        // 选中的胶料品号项参数详情
        private ObservableCollection<Set001> _selectedSet001 = new ObservableCollection<Set001>();
        public ObservableCollection<Set001> SelectedSet001
        {
            get => _selectedSet001;
            set
            {
                _selectedSet001 = value;
                RaisePropertyChanged("SelectedSet001");
            }
        }

        // 选中的胶料品号项--查询本地的同步记参数详情
        public void LoadStrandards(string name1)
        {
            try
            {
                // 从数据库查询头表数据
                var strandard = _sqliteCtx.Db.Queryable<Sqlite_KJ_TEST_STANDARD>().Where(item=>item.PART_NUMBER == name1).ToList();
                Standard = new ObservableCollection<Sqlite_KJ_TEST_STANDARD>(strandard);
            }
            catch (Exception ex)
            {
                _logger.Warn("未查询到MES标准数据",ex);
            }
        }


        public void LoadBas001()
        {
            try
            {
                // 查询胶品号清单
                var bas001 = _accessCtx.Db.Queryable<Bas001>().OrderBy(w => w.Name1).ToList();
                Bas001 = new ObservableCollection<Bas001>(bas001);
            }
            catch (Exception ex)
            {
                _logger.Warn("未查询到胶品号清单", ex);
            }
        }


        // 胶料详细参数
        public void LoadJoinSet001(int Idnum)
        {
            // 多表联查
            try
            {
                 var reslist =_accessCtx.Db.Select<Set001, Tag001, Bas001>()
                 .LeftJoin(w => w.t1.Tagnum == w.t2.Tagnum)
                 .LeftJoin(w => w.t1.Idnum == w.t3.Idnum)
                 .Where(w => w.t1.Idnum == Idnum)
                 .ToList(w => new { w.t1, w.t2, w.t3 });

                var setList = reslist
                    .Select(item =>
                    {
                        var set001 = item.t1;
                        set001.Description = item.t2?.Description;
                        set001.Tagname = item.t2?.Tagname;
                        set001.Unit = item.t2?.Unit;
                        return set001; 
                    })
                
                    .ToList();
                SelectedSet001 = new ObservableCollection<Set001>(setList);
            }
            catch (Exception ex)
            {
                _logger.Warn("未查询到胶品号详情", ex);
            }
        }
       
        public void SearchBas001()
        {

             SerialPortService.Instance.Connect();
             SerialPortService.Instance.SendCommand("0105000103009CFA");
            /*var notification = new CustomNotificationWindow
            {
                NotificationTitle = "测试开班检点提示",
                NotificationContent = "测试提示框内容"
            };*/
            //notification.ShowDialog();
        }
    }
}
